Skip to content

Latest commit

 

History

History
27 lines (26 loc) · 803 Bytes

2019-01-25-Function Oriented(Calling Function).markdown

File metadata and controls

27 lines (26 loc) · 803 Bytes
layout title description date categories comments
post
Function Oriented - Calling Function
Function Oriented - Calling Function
2019-01-25 12:10:00 -0800
Javascript
true

우리가 사용하는 함수들은 모두 Function 객체의 인스턴스이다. 때문에 Function 객체의 내장 메소드들을 사용가능한데, 호출에 사용되는 메소드는 apply와 call이 있다.

function sum(arg1, arg2) {
    return arg1 + arg2;
}
console.log(sum.apply(null, [1, 2]));

o1 = {val1 : 1, val2 : 2, val3 : 3}
o2 = {v1 : 10, v2 : 50, v3 : 100, v4 : 25}
function sum(){
    var _sum = 0;
    for(name in this){ // this에 객체를 인자로 전달
        _sum += this[name];
    }
    return _sum;
}
console.log(sum.apply(o1)) // 6
console.log(sum.apply(o2)) // 185